package com.longagoewk.springboot.controller.manageController;

import com.longagoewk.springboot.entity.MuseumBean;
import com.longagoewk.springboot.entity.UserBean;
import com.longagoewk.springboot.service.CommonService;
import com.longagoewk.springboot.service.MuseumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Controller
public class M_museumController {

    @Autowired
    MuseumService museumService;

    @Autowired
    CommonService commonService;


    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/M_museum")
    public ModelAndView mMuseum(){
        ModelAndView view = new ModelAndView();
        MuseumBean museumBean = getMuseum();
        view.addObject("museumBean",museumBean);
        view.setViewName("manage/M_museum");
        return view;
    }

    @RequestMapping("/editMuseum")
    public ModelAndView editMuseum(){
        ModelAndView view = new ModelAndView();
        MuseumBean museumBean = getMuseum();
        view.addObject("museumBean",museumBean);
        view.setViewName("manage/M_changeMuseum");
        return view;
    }

    @RequestMapping("/updataMuseum")
    public ModelAndView updataMuseum(MuseumBean museumBean,@RequestParam(value = "museumImage") MultipartFile file) throws Exception{
        ModelAndView view = new ModelAndView();
        String fileName = file.getOriginalFilename();  // 文件名
        String type = "/image";
        String folder = "museum";
        if (!fileName.equals("")) {
            fileName = UUID.randomUUID() + fileName;
            commonService.UploadPictures(file, type ,folder, fileName);
            museumBean.setMimage("/image/museum/" + fileName);
        }
        if (museumService.updateUser(museumBean)){
            view.addObject("museumBean",museumBean);
            view.setViewName("manage/M_museum");
        }
        return view;

    }

    public MuseumBean getMuseum(){
        UserBean userBean = (UserBean) request.getSession().getAttribute("user");
        MuseumBean museumBean = new MuseumBean();
        museumBean.setmID(userBean.getmID());
        museumBean = museumService.getMuseum(museumBean);
        return museumBean;
    }
}
